<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core' %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%
    /**
     * @Description :  게시판관리 상세폼
     * @Modification Information
     *
     *   수정일               수정자                   수정내용
     *  -------    --------    ---------------------------
     *
     *
     */
%>
<script type="text/javascript">
$(document).ready(function() {

//********************************************************
// event handler area   
//********************************************************  
    $("#btnRegist").click(function() {
        event.preventDefault();
//TODO Dialog로 공통화...        
        if(confirm("등록하시겠습니까??")) {
            $("#form1").prop("action",'<c:url value="/adm/admBoardMngr/registBoard"/>');
            $("#form1").submit();
        }
    }); 
    $("#btnUpdate").click(function() {
        event.preventDefault();
        
        if(confirm("수정하시겠습니까??")) {
            $("#form1").prop("action",'<c:url value="/adm/admBoardMngr/updateBoard"/>');
            $("#form1").submit();
        }    
    }); 
    $("#btnDelete").click(function() {
        event.preventDefault();
        
        if(!$("#bbs_id").val().length) {alert("선택한 행이 없습니다."); return false;}
        if(confirm("삭제하시겠습니까??")) {
            $("#form1").prop("action",'<c:url value="/adm/admBoardMngr/deleteBoard"/>');
            $("#form1").submit();
        }    
    }); 

    $("#atch_attr_yn").change(function() { // 첨부기능 체크이벤트 발생시 파일옵션 체크
        if(this.checked) {
            $("#divAtch").removeClass("hide");
            $("#divAtch").addClass("view");
            
            // rules추가
            vRules.addRules("#atch_attr_cnt", "첨부파일갯수");
            vRules.addRules("#atch_attr_ext", "확장자유형");
            vRules.addRules("#atch_limit_size", "제한용량(MB)",{
                required: true,
                number: true,
                messages: {
                    required: formatStr("{0}은(는) 필수입력입니다.","제한용량(MB)"),
                    number: formatStr("{0}은(는) 숫자만 입력가능합니다.","제한용량(MB)"),
                }
            });            
            
        } else {
            $("#divAtch").removeClass("view");
            $("#divAtch").addClass("hide");
            
            // rules제거
            vRules.removeRules("#atch_attr_cnt");
            vRules.removeRules("#atch_attr_ext");
            vRules.removeRules("#atch_limit_size");              
            
            // 에러메시지 초기화
            $("label[for='atch_attr_cnt'].error, label[for='atch_attr_ext'].error, label[for='atch_limit_size'].error").remove();
            
        }
    });    
    
//********************************************************
// Validate define area   
//********************************************************     
    $("#form1").validate({
        rules: {},
        messages:{},
        //submitHandler: Form1.registForm,
        showErrors:function(errorMap, errorList){
            this.defaultShowErrors();
        },
        invalidHandler: function(event, validator) {
            var errors = validator.numberOfInvalids();
            if (errors) {
              var message = errors == 1
                ? 'You missed 1 field. It has been highlighted'
                : 'You missed ' + errors + ' fields. They have been highlighted';
              alert(validator.errorList[0].message);
            }
        } ,
    });  
    
    // !! load 함수로 url호출하여 받은 컨텐츠를 partial하게 변경한 경우 
    //    docuemnt ready 이벤트에서
    //    폼요소에 값을 세팅하면 안되고 load콜백 함수에서 처리해야 한다.
    //    사용법에 주의하여 아래와 같이 load플래그를 써서 분기 처리
    if(firstLoad) {
    	// 페이지초기화
        initPage();
    }
});
   

//********************************************************
// page init/finalize area   
//********************************************************
var vRules; // ValidateRules object

/**
 * initialize page 
 */ 
var initPage = function() {
//***********************************************
// validation rules setup
//***********************************************	 
    vRules = new ValidateRules();
    vRules.addRules("#bbs_name", "게시판명");
    vRules.addRules("#bbs_desc", "게시판설명");
    vRules.addRules("#bbs_type", "게시판종류");
    vRules.addRules("#bbs_attr", "게시판속성");
    vRules.addRules("#list_grade", "목록권한");
    vRules.addRules("#read_grade", "읽기권한");
    vRules.addRules("#write_grade", "쓰기권한");
    vRules.addRules("#cmmt_grade", "댓글권한");
    vRules.addRules("#tmpl_id", "템플릿정보");
    // date from~to 비교
    vRules.addRules("#view_stdt, #view_eddt", "게시판 사용기간",{
        required: true,
        compareDate: {fromDate:"#view_stdt", toDate:"#view_eddt"},
        messages: {
            required: formatStr("{0}은(는) 필수입력입니다.","게시판 사용기간"),
            compareDate: formatStr("{0}은(는) 시작일자가 종료일자보다 클수 없습니다.","게시판 사용기간"),
        }
    });         
    
//***********************************************
// 폼컨트롤 setup
//***********************************************

//@TODO 캘린더 공통화
    // calendar 
    $( "#view_stdt, #view_eddt" ).datepicker({
        dateFormat: 'yy/mm/dd',
        showOn: "button",
        buttonImage: "<c:url value='/resources/images/calendar_blue.png'/>",
        buttonImageOnly: true,
        changeMonth: true, // 월선택 select box 표시 (기본은 false)
        changeYear: true,  // 년선택 selectbox 표시 (기본은 false)
        showButtonPanel: true, // 하단 today, done  버튼기능 추가 표시 (기본은 false) 
    });    
    var view_stdt = yyyymmdd('${item.VIEW_STDT}');
    var view_eddt = '${item.VIEW_EDDT}'=='' ? '9999/12/31':yyyymmdd('${item.VIEW_EDDT}');    
    $("#view_stdt").val(view_stdt);
    $("#view_eddt").val(view_eddt);     
    
    // input[type='select']
    // common code 
    // - data: 공통코드ID
    // - selector: selector
    // - selectedValue: 선택되어야 하는 값
    var options = { // 게시판종류
        data:"BBS_TYPE",
        selector: "#bbs_type", 
        selectedValue: '${item.BBS_TYPE}', 
    };
    ajaxCommonCode(options);
    options = { // 게시판속성
        data:"BBS_ATTR",
        selector: "#bbs_attr",
        append: true, 
        selectedValue: '${item.BBS_ATTR}',
    };
    ajaxCommonCode(options);
    options = { // 페이지당목록갯수
        data:"PAGE_PER_SIZE",
        selector: "#page_atcl_cnt",
        selectedValue: '${item.PAGE_ATCL_CNT}',
    };
    ajaxCommonCode(options);
    options = { // 게시판이용등급 !!단일코드로 여러 엘리먼트에 setup하는 case
        data:"MEM_GRADE",
        success:commonCodeCallback,
    };
    ajaxCommonCode(options);
    options = { // 사용여부
        data:"FLAG_YN",
        selector:"#use_yn",
        selectedValue: '${item.USE_YN}',        
    };
    ajaxCommonCode(options);
    options = { // 사용여부
        data:"ATCH_ATTR_CNT",
        selector:"#atch_attr_cnt",
        selectedValue: '${item.ATCH_ATTR_CNT}',        
    };
    ajaxCommonCode(options);
    
    // input[type='checkbox']
    if('${item.ATCH_ATTR_YN}' == 'Y') {
        $("#divAtch").removeClass("hide");
        $("#divAtch").addClass("view");
    } else {
    	$("#divAtch").removeClass("view");
        $("#divAtch").addClass("hide");
    }
};

// select박스 setup 콜백
var commonCodeCallback = function(data, state) {
    respCodeSelect("#list_grade", data, state, false, '${item.LIST_GRADE}');
    respCodeSelect("#read_grade", data, state, false, '${item.READ_GRADE}');
    respCodeSelect("#write_grade", data, state, false, '${item.WRITE_GRADE}');
    respCodeSelect("#cmmt_grade", data, state, false, '${item.CMMT_GRADE}');
};

/**
 * finalize page 
 */ 
var finalizePage = function() {
}; 


//********************************************************
// form validation area   
//********************************************************  
$.validator.setDefaults({
    debug: false,
    ignore: [], // hidden 필드 체크하기 위한 옵션
});   
</script>
${item}
<form id="form1" name="form1" method="post">
    <h3>게시판 생성</h3>
    <table width="100%" border="0" cellpadding="0" cellspacing="1" summary="게시판명,게시판 소개,게시판 유형,게시판 속성,답장가능여부, ..   입니다">
        <colgroup>
            <col width="14%"/>
            <col width="16%"/>
            <col width="20%"/>
            <col width="14%"/>
            <col width="16%"/>
            <col width="20%"/>
        </colgroup>
        <tbody>
            <tr>
                <td>
                    <label for="bbs_id">게시판ID</label>
                </td>
                <td colspan="5">
                    <input id="bbs_id" name="bbs_id" type="text" readonly="readonly" value="${item.BBS_ID}"/>
                </td>
            </tr>    
            <tr>
                <td>
                    <label for="bbs_name">게시판명</label>
                </td>
                <td width="90%" colspan="5">
                    <input id="bbs_name" name="bbs_name" type="text" value="${item.BBS_NAME}"/>
                </td>
            </tr>    
            <tr>
                <td>
                    <label for="bbs_desc">게시판설명</label>
                </td>
                <td colspan="5">
                    <textarea id="bbs_desc" name="bbs_desc" placeholder="여기에 설명을 적으세요" style="width:50%;height:100px;" ><c:out value="${item.BBS_DESC}" escapeXml="true" /></textarea>
                </td>
            </tr>    
            <tr>
                <td >
                    <label for="bbs_type">게시판종류</label>
                </td>
                <td colspan="2">
                    <select id="bbs_type" name="bbs_type">
                    </select>
                </td>
                <td >
                    <label for="bbs_attr">게시판속성</label>
                </td>
                <td colspan="2">
                    <select id="bbs_attr" name="bbs_attr">
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="bbs_url">제공URL</label>
                </td>
                <td colspan="5">
                    <input id="bbs_url" name="bbs_url" type="text" style="width:100%" readonly="readonly" value="${item.BBS_URL}"/>
                </td>
            </tr>                   
            <tr>
                <td colspan="6">
                    <fieldset>
                        <legend>게시판기능 선택</legend>
                        <input type="checkbox" id="atch_attr_yn" name="atch_attr_yn" <c:if test="${item.ATCH_ATTR_YN == 'Y'}">checked="checked"</c:if>/>
                        <label for="atch_attr_yn" class="inline">첨부기능</label>        
                        <input type="checkbox" id="reply_attr_yn" name="reply_attr_yn" <c:if test="${item.REPLY_ATTR_YN == 'Y'}">checked="checked"</c:if>/>
                        <label for="reply_attr_yn" class="inline">답변기능</label>        
                        <input type="checkbox" id="cmmt_attr_yn" name="cmmt_attr_yn" <c:if test="${item.CMMT_ATTR_YN == 'Y'}">checked="checked"</c:if>/>
                        <label for="cmmt_attr_yn" class="inline">댓글기능</label>        
                        <input type="checkbox" id="hidd_attr_yn" name="hidd_attr_yn" <c:if test="${item.HIDD_ATTR_YN == 'Y'}">checked="checked"</c:if>/>
                        <label for="hidd_attr_yn" class="inline">숨김글기능</label>        
                        <input type="checkbox" id="tag_attr_yn" name="tag_attr_yn" <c:if test="${item.TAG_ATTR_YN == 'Y'}">checked="checked"</c:if>/>
                        <label for="tag_attr_yn" class="inline">태그기능</label>        
                        <input type="checkbox" id="cate_attr_yn" name="cate_attr_yn" <c:if test="${item.CATE_ATTR_YN == 'Y'}">checked="checked"</c:if>/>
                        <label for="cate_attr_yn" class="inline">카테고리기능</label>        
                        <input type="checkbox" id="rcmd_attr_yn" name="rcmd_attr_yn" <c:if test="${item.RCMD_ATTR_YN == 'Y'}">checked="checked"</c:if>/>
                        <label for="rcmd_attr_yn" class="inline">글추천기능</label>        
                        <input type="checkbox" id="atcl_dt_attr_yn" name="atcl_dt_attr_yn" <c:if test="${item.ATCL_DT_ATTR_YN == 'Y'}">checked="checked"</c:if>/>
                        <label for="atcl_dt_attr_yn" class="inline">게시글기간설정기능</label>        
                    </fieldset>
                    <fieldset id="divAtch" class="hide">
                        <legend>첨부기능 입력</legend>
                        <label for="atch_attr_cnt" class="inline">첨부파일갯수</label>
                        <select id="atch_attr_cnt" name="atch_attr_cnt">
                        </select>
                        <label for="atch_attr_ext" class="inline">확장자유형</label>
                        <input type="text" id="atch_attr_ext" name="atch_attr_ext" value="${item.ATCH_ATTR_EXT}"/>
                        <label for="atch_limit_size" class="inline">제한용량(MB)</label>                                
                        <input type="text" id="atch_limit_size" name="atch_limit_size" value="${item.ATCH_LIMIT_SIZE}"/>
                    </fieldset>
                </td>
            </tr>                   
            <tr>
                <td colspan="6">
                    <fieldset>
                        <legend>게시판 이용권한</legend>
                        <label for="list_grade">목록권한</label>    
                        <select id="list_grade" name="list_grade">
                        </select>     
                        <label for="read_grade">읽기권한</label>    
                        <select id="read_grade" name="read_grade">
                        </select>     
                        <label for="write_grade">쓰기권한</label>    
                        <select id="write_grade" name="write_grade">
                        </select>     
                        <label for="cmmt_grade">댓글권한</label>    
                        <select id="cmmt_grade" name="cmmt_grade">
                        </select>     
                    </fieldset>
                </td>
            </tr> 
            <tr>
                <td>
                    <label for="page_atcl_cnt">페이지당 글갯수</label>
                </td>
                <td colspan="5">
                    <select id="page_atcl_cnt" name="page_atcl_cnt">
                    </select>
                </td>
            </tr>                                 
            <tr>
                <td>
                    <label for="tmpl_id">템플릿정보</label>
                </td>
                <td colspan="5">
                    <input id="tmpl_id" name="tmpl_id" class="disabled" type="text" style="width:80%"  value="${item.TMPL_ID}"/>
                </td>
            </tr>                                 
            <tr>
                <td>
                    <label for="">게시판 사용기간</label>
                </td>
                <td colspan="5">
                    
                    <input id="view_stdt" name="view_stdt" type="text" readonly="readonly"  value=""/> ~ 
                    <input id="view_eddt" name="view_eddt" type="text" readonly="readonly"  value=""/>
                </td>
            </tr>                                 
            <tr>
                <td>
                    <label for="">사용여부</label>
                </td>
                <td colspan="5">
                    <select id="use_yn" name="use_yn">
                    </select>  
                </td>
            </tr>                                 
        </tbody>
    </table>
</form>

<div class="col_12 center">
    <button id="btnRegist">등록</button>
    <button id="btnUpdate">수정</button>
    <button id="btnDelete">삭제</button>
</div>